/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.physics;

import hapi.math.Additive;
import hapi.math.Multiplicative;

class Time implements Additive<Time,Time>, implements Multiplicative<Time,Float> {

	public var seconds(default, null):Float;
	public var millis(getMillis, null):Int;
	public var minutes(getMinutes, null):Float;
	public var hours(getHours, null):Float;
	
	private static inline var SEC_TO_MIN = 0.016666666666666666666666666666666666666666666666667;
	private static inline var SEC_TO_HOURS = 2.7777777777777777777777777777777777777777777777778e-4;
	
	public static function noTime() {
		return new Time(0.0);
	}
	
	public static function now() {
		return new Time(haxe.Timer.stamp());
	}
	
	public function new(seconds:Float) {
		this.seconds = seconds >= 0 ? seconds : 0;
	}
	
	public function add(time:Time):Time {
		seconds += time.seconds;
		return this;
	}
	
	public function substract(time:Time):Time {
		if (lessThan(time)) 
			seconds = 0.0;
		else
			seconds -= time.seconds;
		return this;
	}
	
	public function multiplyBy(coef:Float):Time {
		if(coef > 0)
			seconds *= coef;
		else
			seconds = 0.0;
		return this;
	}
	
	public function divideBy(coef:Float):Time {
		return multiplyBy(1 / coef);
	}
	
	public function lessThan(t:Time):Bool {
		return this.seconds < t.seconds;
	}
	
	public function moreThan(t:Time):Bool {
		return this.seconds > t.seconds;
	}
	
	public function isNull():Bool {
		return seconds == 0.0;
	}
	
	public function clone():Time {
		return new Time(seconds);
	}
	
	public function toString() {
		return seconds + " seconds";
	}
	
	function getMillis() {
		return Std.int(seconds * 1000);
	}
	
	function getMinutes() {
		return seconds * SEC_TO_MIN;
	}
	
	function getHours() {
		return seconds * SEC_TO_HOURS;
	}
}